package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.AddressMapper;
import com.itheima.pojo.Address;
import com.itheima.pojo.Setmeal;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired private AddressMapper addressMapper;
    @Autowired private JedisPool jedisPool;

    /**
     * 分页查询，机构地址信息
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = addressMapper.findByCondition(queryString);
        List<Setmeal> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,result);
    }

    /**
     * 获取所有机构地址图片名称
     * @return
     */
    public List<String> getAllImg() {
        return addressMapper.getAllImg();
    }

    /**
     * 新增机构地址
     * @param address
     */
    public void add(Address address) {
        addressMapper.add(address);
        String fileName = address.getImg();
        if (fileName != null) {
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);
            //释放资源
            resource.close();
        }
    }

    /**
     * 编辑机构地址信息
     * @param address
     */
    public void edit(Address address) {
        addressMapper.edit(address);
    }

    /**
     * 删除机构地址信息
     * @param id
     */
    public void deleteById(Integer id) {
        addressMapper.deleteById(id);
    }

    /**
     * 查询所有地址信息
     * @return
     */
    public List<Address> findAllAddress() {
        return addressMapper.findAllAddress();
    }

    @Override
    public String findLatLngById(Integer id) {
        return addressMapper.findLatLngById(id);
    }
}
